#include "server_map.h"
using namespace std;

namespace PFS
{

    ServerMap:: ~ServerMap()
    {
        server_map.clear();
    }

    void ServerMap::add_server(const uint64_t server_id, ServerLocate* serv)
    {
        SERVER_MAP_ITER it = server_map.find(server_id);
        if(it == server_map.end())
            server_map[server_id] = serv;
        else
            return;
    }

    ServerLocate* ServerMap::replace_server(const uint64_t server_id, ServerLocate* new_serv)
    {
        ServerLocate *serv = NULL;
        SERVER_MAP_ITER it = server_map.find(server_id);
        if(it != server_map.end())
        {
            serv = it->second;
            it->second = new_serv;
        }
        else
            server_map[server_id] = new_serv;
        return serv;
    }

    void ServerMap::remove_server(const uint64_t server_id)
    {
        SERVER_MAP_ITER it = server_map.find(server_id);
        if( it != server_map.end())
        {
            delete id->second;
            server_map.erase(server_id);
        }
    }

    ServerLocate* ServerMap::find_server(const uint64_t server_id)
    {
        SERVER_MAP_ITER it = server_map.find(server_id);
        if( it != server_map.end())
            return it->second;
        else
            return NULL;
    }

    bool ServerMap::exsit_server(const uint64_t server_id) const
    {
        return (server_map.count(server_id) != 0);
    }

    char* ServerMap::get_server_addr(const uint64_t server_id)
    {
        ServerLocate* serv = find_server(server_id);
        if(NULL == serv)
            return NULL;
        else
            return serv->get_date_server()->addr.c_str();
    }
}
